﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMPLib;
using System.Media;

namespace WindowsFormsApplication1
{
    public partial class game : Form
    {
        public game()
        {
            InitializeComponent();

        }
        public Form backForm { get; set; }
        
        scorescreen frm3 = new scorescreen();
        SoundPlayer hitPlayer = new SoundPlayer(@"Sounds\hitsound.wav");
        SoundPlayer three = new SoundPlayer(@"Sounds\3.wav");
        SoundPlayer two = new SoundPlayer(@"Sounds\2.wav");
        SoundPlayer one = new SoundPlayer(@"Sounds\1.wav");
        SoundPlayer begin = new SoundPlayer(@"Sounds\begin.wav");

        int score, combo, enb,clickcount;
        DateTime gamestarttime;
        DateTime gameendtime;
        int lastclick = 0;
        WindowsMediaPlayer musicplayer;
        string musicurl;
        bool isGamestarted = false;
        bool clicked = false;
        const int font_max_size = 30;
        float font_min_size;
        float countdown_font_min_size;
        int countdownstate = 3;
      
        private void timer1_Tick(object sender, EventArgs e)
        {
            
            TimeSpan diff = gameendtime - DateTime.Now;
            timepastlabel.Text = string.Format("{0}:{1}:{2}", diff.Duration().Minutes, diff.Duration().Seconds, diff.Duration().Milliseconds);
            if (diff.TotalMilliseconds < 0)
            {
                onGameEnd();
            
            }
        }
        private void onGameEnd()
        {
            gameLength.Stop();
            adiolmayantimer.Stop();//başka ne bug var eğer yeni circle çıkarsa ve tıklamazsak combo sıfırlanmıyo
            newTimer.Stop();
            if(enb > 0)
                score *= enb;
            timepastlabel.Text = "0:00:00";
            int cscore = ((int)Properties.Settings.Default["gameScore"]);
            if (cscore < score)
                Properties.Settings.Default["gameScore"] = score;
            Properties.Settings.Default.Save();
            frm3.skor = ((int)Properties.Settings.Default["gameScore"]);
            frm3.enb = enb;
            frm3.Show();

            this.Hide();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            musicplayer = new WindowsMediaPlayer();
            gamestarttime = new DateTime();
            gameendtime = new DateTime();
            clickcount = 1;
            FormBorderStyle = FormBorderStyle.None;
            WindowState = FormWindowState.Maximized;
            if ((bool)Properties.Settings.Default["gameBGHide"] == true)
                this.BackgroundImage = null;
            font_min_size = label2.Font.Size;
            countdown_font_min_size = countdownlbl.Font.Size;
            this.KeyPreview = true;
            countdown.Start();
            countdownAnimTimer.Start();
        }
        
        private void targetButton_Click(object sender, EventArgs e)
        {
            if (clicked)
                return;
            lastclick = Environment.TickCount;
            clicked = true;
            adiolmayantimer.Start();
            ((PictureBox)sender).Visible = false;
            newTimer.Start();
            Random rand = new Random();
            clickcount++;      
            
            score += 300;
            combo++;
            if (enb < combo)
            {
                enb = combo;
            }
            if((bool) Properties.Settings.Default["gameHitSound"] == false)
                 hitPlayer.Play();
            label1.Text = score.ToString();
            label2.Text = combo + "x";
            dectimer.Stop();
            inctimer.Start();
        }

        private void adiolmayantimer_Tick(object sender, EventArgs e)
        {     
            Random rand = new Random();
            circle.Location = new Point(rand.Next(0, this.Width - circle.Width),
                rand.Next(0, this.Height - circle.Height));
            clicked = false;
            if (Environment.TickCount - lastclick > adiolmayantimer.Interval)// burda sorun var ozaman
            {
                combo = 0;
                label2.Text = combo.ToString() + "x";              
            }           
        }

        private void newTimer_Tick(object sender, EventArgs e)
        {
            circle.Visible = true;// bura delay veriyo
            newTimer.Stop();
        }

        private void musicselectB_Click(object sender, EventArgs e)
        {
            OpenFileDialog d = new OpenFileDialog();
            d.Multiselect = false;
            d.ShowDialog();
            if (!string.IsNullOrEmpty(d.FileName))
                musicurl = d.FileName;
        }

        private void Form1_Click(object sender, EventArgs e)
        {
            if (!isGamestarted || clicked)
                return;
            combo = 0;
            label2.Text = combo + "x";
       
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            int width = this.Width;
            int height = this.Height;
            label1.Location = new Point(width - Convert.ToInt32( width*0.2), label1.Location.Y);
            label2.Location = new Point(label2.Location.X, height -Convert.ToInt32( height * 0.05));        
            countdownlbl.Location = new Point((width / 2) - (countdownlbl.Width / 2), (height / 2) - (countdownlbl.Height / 2));
        }

        private void game_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((string)Properties.Settings.Default["gameHotKey"] == e.KeyChar.ToString().ToUpper() && circle.Visible)
            {
                if (this.ClientRectangle.Contains(circle.PointToClient(MousePosition)))
                    targetButton_Click(circle, null);
            }
        }

        private void inctimer_Tick(object sender, EventArgs e)
        {
            if (label2.Font.Size < font_max_size)
            {
                label2.Font = new Font(label2.Font.FontFamily, label2.Font.Size + 1);
            }
            
            if (label2.Font.Size == font_max_size)
            {
                ((Timer)sender).Stop();
                dectimer.Start();
            }
        }

        private void dectimer_Tick(object sender, EventArgs e)
        {
            if (label2.Font.Size > font_min_size)
                label2.Font = new Font(label2.Font.FontFamily, label2.Font.Size - 1);
        }

        private void countdown_Tick(object sender, EventArgs e)
        {
            switch (countdownstate)
            {
                case 3: three.Play(); break;
                case 2: two.Play(); break;
                case 1: one.Play(); break;
                case 0: begin.Play(); break;
            }
            
            countdownlbl.Font = new Font(countdownlbl.Font.FontFamily, countdown_font_min_size);
            if (countdownlbl.Text == "GO!")
            {
                ((Timer)sender).Stop();
                countdownlbl.Visible = false;
                gameStartTimer.Start();
                       
            }
            int width = this.Width;
            int height = this.Height;
            countdownlbl.Text = countdownstate.ToString();
            countdownlbl.Location = new Point((width / 2) - (countdownlbl.Width / 2), (height / 2) - (countdownlbl.Height / 2));
            countdownstate--;
            
            if (countdownstate < 0)
            {
                countdownlbl.Text = "GO!";

                if ((bool)Properties.Settings.Default["gameMusic"] == false)
                {
                    musicplayer.URL = AppDomain.CurrentDomain.BaseDirectory + @"\Freedom Dive.mp3";
                    musicplayer.controls.play();
                }
            }

        }

        private void gameStartTimer_Tick(object sender, EventArgs e)
        {
            countdownlbl.Visible = false;
            gamestarttime = DateTime.Now;
            gameendtime = DateTime.Now.AddSeconds(138);
            gameLength.Start();
            adiolmayantimer.Start();
            clickcount = 1;
            circle.Visible = true;
            isGamestarted = true;
            countdown.Stop();
        }

        private void countdownAnimTimer_Tick(object sender, EventArgs e)
        {
            if (countdownlbl.Font.Size < font_max_size+60)
            {
                countdownlbl.Font = new Font(countdownlbl.Font.FontFamily, countdownlbl.Font.Size + 1);
            }

        }                 
        }
    }

